package mid

func MinimumRefill(plants []int, capacityA int, capacityB int) int {
	cnt := 0
	totalA, totalB := capacityA, capacityB
	aSize, bSize := 0, len(plants)-1
	for aSize < len(plants) && bSize >= 0 && aSize <= bSize {
		if aSize != bSize {
			if capacityA >= plants[aSize] {
				capacityA -= plants[aSize]
			} else {
				cnt++
				capacityA = totalA - plants[aSize]
			}
			if capacityB >= plants[bSize] {
				capacityB -= plants[bSize]
			} else {
				cnt++
				capacityB = totalB - plants[bSize]
			}
			aSize++
			bSize--
		} else {
			if capacityA >= plants[aSize] {
				capacityA -= plants[aSize]
				aSize++
			} else if capacityB >= plants[aSize] {
				capacityB -= plants[aSize]
				bSize--
			} else {
				cnt++
				capacityA = totalA - plants[aSize]
				aSize++
			}
		}
	}
	return cnt
}
